package com.zclcs.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zclcs.model.CurrentUser;
import com.zclcs.model.DataRet;
import com.zclcs.model.Menu;
import com.zclcs.model.User;
import com.zclcs.service.UserService;
import com.zclcs.utils.ServletContextUtil;

/**
 * 用户控制器 GET 用来获取资源， POST 用来新建资源（也可以用于更新资源）， PUT 用来更新资源， DELETE 用来删除资源。
 * 
 * @author 周成龙
 *
 */
@RestController
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 登录
	 * 
	 * @param user
	 * @return
	 */
	@PostMapping(value = "/user/login")
	public DataRet<Object> login(User user) {
		DataRet<Object> dataRet = new DataRet<Object>();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			dataRet.setMessage("登录成功");
			dataRet.setData(ServletContextUtil.getCurrentUser());
		} catch (IncorrectCredentialsException e) {
			e.printStackTrace();
			dataRet.setCode(9999999);
			dataRet.setMessage("用户名或密码错误");
		} catch (AuthenticationException e) {
			e.printStackTrace();
			dataRet.setCode(9999999);
			dataRet.setMessage("用户名或密码错误");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataRet;
	}

	/**
	 * 登出
	 * 
	 * @param user
	 * @return
	 */
	@PostMapping(value = "/user/logout")
	public void logout() {

		Subject subject = SecurityUtils.getSubject();
		subject.logout();

	}

	/**
	 * 注册
	 * 
	 * @return
	 */
	@RequestMapping(value = "/user/register")
	public void register(User user) {
		userService.insertUser(user);
	}

	/**
	 * 修改密码
	 * 
	 * @param newPassword 新密码
	 * @param oldPassword 旧密码
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/user/changePassword")
	public DataRet<Object> changePassword(String newPassword, String oldPassword) throws Exception {
		return userService.changePassword(newPassword, oldPassword);
	}

	/**
	 * 获取用户权限
	 * 
	 * @return
	 */
	@GetMapping(value = "/user/info")
	public DataRet<CurrentUser> getCurrentUserPermission() {

		return new DataRet<CurrentUser>(ServletContextUtil.getUserInfo());

	}

	/**
	 * 获取用户路由权限
	 * 
	 * @return
	 */
	@GetMapping(value = "/user/getServerRoute")
	public DataRet<List<Menu>> getCurrentUserMenu() {

		return new DataRet<List<Menu>>(ServletContextUtil.getServerRoute());

	}

	@GetMapping(value = "/user/selectUserPage")
	public DataRet<IPage<User>> selectUserPage(User user) {

		return new DataRet<IPage<User>>(userService.selectUserPage(user));

	}

	@PostMapping(value = "/user/insertUser")
	public void insertUser(User user) {

		userService.insertUser(user);

	}

	@PutMapping(value = "/user/updateUser")
	public void updateUser(User user) {

		userService.updateUser(user);

	}

	@DeleteMapping(value = "/user/deleteUser")
	public DataRet<Object> deleteUser(@RequestParam(value = "ids[]") List<String> ids) {

		return userService.deleteUser(ids);

	}

	@PostMapping(value = "/user/validUsername")
	public DataRet<Object> validUsername(User user) {

		return new DataRet<Object>(userService.validUsername(user));

	}

	@PutMapping(value = "/user/resetPassword")
	public void resetPassword(User user) {

		userService.resetPassword();

	}

}
